package com.ssyc.common.manager;

import android.widget.Chronometer;

/**
 * 计时器控件的管理类
 * Created by Administrator on 2018/5/11 0011.
 */

public class TimerCountManager {

    /**
     * 开启计时器的方法
     *
     * @param isFirst 是否需要断点计时的标记,true不需要,一般代表第一次开始计时,false需要断点计时,也就是上次退出时的时间开始计时
     *                解决在APP进入后台,熄屏后,重新进入前台或者亮屏状态下继续计时
     * @param timer   计时器控件
     */
    public static void startTimer(final boolean isFirst, final Chronometer timer) {
        if (timer != null) {
            if (isFirst) {
                //重新设置计时器格式的代码
                timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
                    @Override
                    public void onChronometerTick(Chronometer cArg) {
                        long time = System.currentTimeMillis() - cArg.getBase();
                        int h = (int) (time / 3600000);
                        int m = (int) (time - h * 3600000) / 60000;
                        int s = (int) (time - h * 3600000 - m * 60000) / 1000;
                        String hh = h < 10 ? "0" + h : h + "";
                        String mm = m < 10 ? "0" + m : m + "";
                        String ss = s < 10 ? "0" + s : s + "";
                        String timeFormat = hh + ":" + mm + ":" + ss;
                        timer.setText(timeFormat);
                    }
                });

                timer.setBase(System.currentTimeMillis());

            } else {
                timer.setBase((long) ((long) (System.currentTimeMillis() - (long) Double
                        .parseDouble(timer.getText().toString().split(":")[0]) * 60 * 60 * 1000) - Double
                        .parseDouble(timer.getText().toString().split(":")[1]) * 60 * 1000) -
                        (long) Double.parseDouble(timer.getText().toString().split(":")[2]) * 1000);
            }
            timer.start();
        }
    }

    /**
     * 停止计时器的方法
     */
    public static void stopTimer(Chronometer timer) {
        if (timer != null) {
            timer.stop();
        }
    }


    /**
     * @param cmt Chronometer控件
     * @return 小时+分钟+秒数  的所有秒数
     */
    public static String getChronometerSeconds(Chronometer cmt) {
        int totalss = 0;
        String string = cmt.getText().toString();
        if (string.length() == 7) {

            String[] split = string.split(":");
            String string2 = split[0];
            int hour = Integer.parseInt(string2);
            int Hours = hour * 3600;
            String string3 = split[1];
            int min = Integer.parseInt(string3);
            int Mins = min * 60;
            int SS = Integer.parseInt(split[2]);
            totalss = Hours + Mins + SS;
            return String.valueOf(totalss);
        } else if (string.length() == 5) {

            String[] split = string.split(":");
            String string3 = split[0];
            int min = Integer.parseInt(string3);
            int Mins = min * 60;
            int SS = Integer.parseInt(split[1]);

            totalss = Mins + SS;
            return String.valueOf(totalss);
        }
        return String.valueOf(totalss);
    }

}
